package net.disy.cadenza.repository.wrappers;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import net.disy.cadenza.dbmapping.schema.AttributeMapping;
import net.disy.cadenza.dbmapping.schema.DatabaseAttribute;
import net.disy.cadenza.dbmapping.schema.DatabaseMapping;
import net.disy.cadenza.objecttypes.schema.Attribute;
import net.disy.cadenza.objecttypes.schema.CadenzaObjectType;
import net.disy.cadenza.objecttypes.schema.ObjectTypeList;

public class ObjectTypeListWrapper {
	private ObjectTypeList objectTypeList;
	
	public ObjectTypeListWrapper(ObjectTypeList objectTypeList){
		this.objectTypeList = objectTypeList;
	}
	
	public List<CadenzaObjectType> getObjectType(){
		return objectTypeList.getObjectType();
	}
	
	public Set<CadenzaObjectType> getObjectTypeForTable(String tableName){
		Set<CadenzaObjectType> objectTypes = new HashSet<CadenzaObjectType>();
		for(CadenzaObjectType objectType : objectTypeList.getObjectType()){
			if(((DatabaseMapping)objectType.getMappings().getAny()).getTable().equalsIgnoreCase(tableName))
				objectTypes.add(objectType);
		}
		return objectTypes;
	}
	
	public Set<String> getObjectTypeAttributeNamesForTableColumn(String tableColumn){
		Set<String> objectTypeAttributeNames = new HashSet<String>();
		String tableName = tableColumn.substring(0, tableColumn.lastIndexOf("."));
		String columnName = tableColumn.substring(tableColumn.lastIndexOf(".") + 1);
		for(CadenzaObjectType objectType : objectTypeList.getObjectType()){
			DatabaseMapping databaseMapping = (DatabaseMapping)objectType.getMappings().getAny();
			if(databaseMapping.getTable().equalsIgnoreCase(tableName)){
				for(AttributeMapping attributeMapping : databaseMapping.getAttributeMapping()){
					if(attributeMapping.getSource() != null && attributeMapping.getSource().getColumn().equalsIgnoreCase(columnName)){
						objectTypeAttributeNames.add(attributeMapping.getAttributeName());
					}
				}
			}
		}
		return objectTypeAttributeNames;
	}
	
	public CadenzaObjectType getObjectType(String objectTypeName){
		for(CadenzaObjectType cadenzaObjectType : objectTypeList.getObjectType()){
			if(cadenzaObjectType.getName().equalsIgnoreCase(objectTypeName)){
				return cadenzaObjectType;
			}
		}
		return null;
	}
	
	public Attribute getObjectTypeAttribute(String objectTypeName, String objectTypeAttributeName){
		CadenzaObjectType cadenzaObjectType = getObjectType(objectTypeName);
		if(cadenzaObjectType != null){
			for(Attribute attribute : cadenzaObjectType.getObjectTypeStructure().getAttributeList().getAttribute()){
				if(attribute.getName().equalsIgnoreCase(objectTypeAttributeName)){
					return attribute;
				}
			}
		}
		return null;
	}
	
	public DatabaseMapping getDatabaseMapping(String objectTypeName){
		CadenzaObjectType cadenzaObjectType = getObjectType(objectTypeName);
		if(cadenzaObjectType != null)
			return (DatabaseMapping)cadenzaObjectType.getMappings().getAny();
		return null;
	}
	
	public List<DatabaseMapping> getDatabaseMappings(){
		List<DatabaseMapping> databaseMappings = new ArrayList<DatabaseMapping>();
		for(CadenzaObjectType cadenzaObjectType : objectTypeList.getObjectType()){
			databaseMappings.add((DatabaseMapping)cadenzaObjectType.getMappings().getAny());
		}
		return databaseMappings;
	}
	
	public DatabaseAttribute getDatabaseAttribute(String objectTypeName, String objectTypeAttributeName){
		DatabaseMapping databaseMapping = getDatabaseMapping(objectTypeName);
		if(databaseMapping != null){
			for(AttributeMapping attributeMapping : databaseMapping.getAttributeMapping()){
				if(attributeMapping.getAttributeName().equalsIgnoreCase(objectTypeAttributeName))
					return attributeMapping.getSource();
			}
		}
		return null;
	}
	
	public List<DatabaseAttribute> getDatabaseAttributes(String objectTypeName){
		DatabaseMapping databaseMapping = getDatabaseMapping(objectTypeName);
		List<DatabaseAttribute> databaseAttributes = new ArrayList<DatabaseAttribute>();
		
		if(databaseMapping != null){
			for(AttributeMapping attributeMapping : databaseMapping.getAttributeMapping()){
				databaseAttributes.add(attributeMapping.getSource());
			}
		}
		
		return databaseAttributes;
	}
	
//	public List<DatabaseAttribute> getKeyDatabaseAttributes(String table){
//		List<DatabaseAttribute> keyDatabaseAttributes = new ArrayList<DatabaseAttribute>();
//		for(CadenzaObjectType cadenzaObjectType : objectTypeList.getObjectType()){
//			if(getDatabaseMapping(cadenzaObjectType.getName()).getTable().equalsIgnoreCase(table)){
//				for(Attribute attribute : cadenzaObjectType.getObjectTypeStructure().getAttributeList().getAttribute()){
//					if(attribute.isSetKey() && attribute.isKey()){
//						DatabaseAttribute databaseAttribute = getDatabaseAttribute(cadenzaObjectType.getName(), attribute.getName());
//						keyDatabaseAttributes.add(databaseAttribute);
//					}
//				}
//			}
//		}
//		return keyDatabaseAttributes;
//	}
}
